package com.peas.platform.module.icims.budget.project.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.budget.project.domain.BudgetProjectNoticeDomain;
import com.peas.platform.module.icims.budget.project.dto.BudgetProjectNoticeDto;
import com.peas.platform.module.icims.budget.project.dto.BudgetProjectNoticeFileDto;
import com.peas.platform.module.icims.budget.project.service.BudgetProjectNoticeFileService;
import com.peas.platform.module.icims.budget.project.service.BudgetProjectNoticeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BudgetProjectNoticeServiceImpl extends BaseServiceImpl<BudgetProjectNoticeDto> implements BudgetProjectNoticeService {

    @Resource
    private BudgetProjectNoticeDomain budgetProjectNoticeDomain;
    @Resource
    private BudgetProjectNoticeFileService budgetProjectNoticeFileService;

    @Override
    public BaseDomain<BudgetProjectNoticeDto, ? extends BasePo> getDomain() {
        return budgetProjectNoticeDomain;
    }

    @Transactional
    @Override
    public int saveBudgetProjectNotice(BudgetProjectNoticeDto dto, List<BudgetProjectNoticeFileDto> files) {
        int result = this.save(dto);
        if (CollUtil.isNotEmpty(files)) {
            files.forEach(budgetProjectNoticeFileDto -> budgetProjectNoticeFileDto.setBudgetProjectNoticeId(dto.getId()));
            budgetProjectNoticeFileService.saveBatch(files);
        }
        return result;
    }

    @Transactional
    @Override
    public int modifyBudgetProjectNotice(BudgetProjectNoticeDto dto, List<BudgetProjectNoticeFileDto> files) {
        int result = this.updateById(dto);
        List<BudgetProjectNoticeFileDto> bpnfds = budgetProjectNoticeFileService.listByField("budgetProjectNoticeId", dto.getId());
        if (CollUtil.isNotEmpty(bpnfds)) {
            budgetProjectNoticeFileService.removeBatchByList(bpnfds);
        }
        if (CollUtil.isNotEmpty(files)) {
            files.forEach(budgetProjectNoticeFileDto -> budgetProjectNoticeFileDto.setBudgetProjectNoticeId(dto.getId()));
            budgetProjectNoticeFileService.saveBatch(files);
        }
        return result;
    }

}
